package com.boot.security.server.controller.mffl.MfflLawfirm;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.dao.mffl.MfflLawfirmDao;
import com.boot.security.server.model.mffl.MfflLawfirm;
import com.boot.security.server.service.impl.mffl.MfflLawfirm.MfflLawfirmService;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/mfflLawfirms")
public class MfflLawfirmController {

    @Autowired
    private MfflLawfirmDao mfflLawfirmDao;

    @Autowired
    private MfflLawfirmService MfflLawfirmService;

    @PostMapping
    @ApiOperation(value = "保存")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:add')")
    public MfflLawfirm save(@RequestBody MfflLawfirm mfflLawfirm) {
        return  MfflLawfirmService.save(mfflLawfirm);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:query')")
    public MfflLawfirm get(@PathVariable Long id) {
        return mfflLawfirmDao.getById(id);
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:update')")
    public MfflLawfirm update(@RequestBody MfflLawfirm mfflLawfirm) {
        mfflLawfirmDao.update(mfflLawfirm);
        return mfflLawfirm;
    }

    @GetMapping
    @ApiOperation(value = "列表")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:query')")
    public PageTableResponse list(PageTableRequest request) {
        request.getParams().put("deleteState",1);
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return mfflLawfirmDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<MfflLawfirm> list(PageTableRequest request) {
                return mfflLawfirmDao.list(request.getParams(), request.getOffset(), request.getLimit());
            }
        }).handle(request);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    @PreAuthorize("hasAnyAuthority('sys:MfflLawfirm:del')")
    public void delete(@PathVariable Long id) {
        mfflLawfirmDao.delete(id);
    }
}
